package service

import (
	"blizzard/app/dao"
	"blizzard/app/model"
	"blizzard/app/utils/tool"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/text/gstr"
	"github.com/gogf/gf/util/gconv"
	"math/rand"
)

var Card = new(serviceCard)

type serviceCard struct {}

func (s *serviceCard) Add(m *model.CardParam) (interface{}, error) {
	var (
		err error
		one *model.BxCardType
	)
	one, err = dao.BxCardType.FindOne("id = ?", m.CardTypeId)
	if err != nil {
		return nil, err
	}
	data := g.Map{}
	data["time"] = one.Time
	data["time_type"] = one.TimeType
	data["software_id"] = one.SoftwareId
	data["name"] = one.Name
	data["create_time"] = tool.GetTime()
	data["menber_id"] = 1
	body := make(map[int]string)
	for i := 0; i < m.Count; i++ {
		body[i] = gstr.ToUpper(tool.Md5String(tool.UniqueId() + gconv.String(rand.Intn(100))))
		data["cardnum"] = body[i]
		_, err = dao.BxCard.Save(data)
		if err != nil {
			return nil, err
		}
	}
	return body, nil
}

func (s *serviceCard) List() (interface{}, error) {
	all, err := dao.BxCard.FindAll("id > ?", 0)
	if err != nil {
		return nil, err
	}
	return all, nil
}